package com.spider.silence.proxy;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;
import com.spider.silence.constant.Constant;
import org.jsoup.Jsoup;
import us.codecraft.webmagic.proxy.Proxy;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * Created by 夏茂轩 on 2016/12/16.
 * @description 代理池
 */
public class ProxyPool {
    private static ProxyPool pool;
    private ProxyPool(){}
    private static LinkedBlockingDeque<String> queue=new LinkedBlockingDeque<String>();
    private static List<String[]> proxyList= Lists.newArrayList();
    private static Proxy[] proxyArray= null;
    private static String url="http://svip.kuaidaili.com/api/getproxy/?orderid=953994536123042&num=100&b_pcchrome=1&b_pcie=1&b_pcff=1&protocol=1&method=2&an_an=1&an_ha=1&sp1=1&quality=2&format=json&sep=1";
    private static  long switchCount=0L;

    /**
     * @author 夏茂轩
     * @date 2017/1/15 22:27
     * @description 同步方法,获取单一代理,格式为:IP:PORT
     * @return String
     */
    public static synchronized  String proxy(){
       if(queue.size()<=0){
           String rts="";
           while (true){
               try {
                   rts=Jsoup.connect(url).timeout(Constant.HTTP_TIME_OUT).ignoreContentType(true).get().text();
                   JSONArray array = JSON.parseObject(rts).getJSONObject("data").getJSONArray("proxy_list");
                   for(int i=0;i<array.size();i++){
                        queue.add(array.getString(i));
                   }
                   break;
               } catch (IOException e) {
                   try {
                       Thread.sleep(1000*2);
                   } catch (InterruptedException e1) {
                       e1.printStackTrace();
                   }
                   e.printStackTrace();
                   continue;
               }
           }
       }
        String proxy="";
        try {
            proxy= queue.takeFirst();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return  proxy;
    }

    /**
     * @author 夏茂轩
     * @date 2017/1/15 22:28
     * @description 获取多个代理
     * @return new String[]{"IP1:PORT1","IP2:PORT2"}
     */
    public static synchronized List<String[]> proxyList(){
        switchCount++;
        if(proxyList.size()==0||switchCount%10000==0){
            String rts="";
            while (true){
                try {
                    rts=Jsoup.connect(url).timeout(Constant.HTTP_TIME_OUT).ignoreContentType(true).get().text();
                    JSONArray array = JSON.parseObject(rts).getJSONObject("data").getJSONArray("proxy_list");
                    for(int i=0;i<array.size();i++){
                        String proxy=array.getString(i);
                        proxyList.add(new String[]{"","",proxy.split(":")[0],proxy.split(":")[1]});
                    }
                    break;
                } catch (IOException e) {
                    try {
                        Thread.sleep(1000*2);
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }
                    e.printStackTrace();
                    continue;
                }
            }
        }
        return  proxyList;
    }


    /**
     * @author 夏茂轩
     * @date 2017/1/15 22:28
     * @description 获取多个代理
     * @return new String[]{"IP1:PORT1","IP2:PORT2"}
     */
    public static synchronized Proxy[] proxyArray(){
        switchCount++;
        if(proxyList.size()==0||switchCount%10000==0){
            String rts="";
            while (true){
                try {
                    rts=Jsoup.connect(url).timeout(Constant.HTTP_TIME_OUT).ignoreContentType(true).get().text();
                    JSONArray array = JSON.parseObject(rts).getJSONObject("data").getJSONArray("proxy_list");
                    proxyArray =new Proxy[array.size()];
                    for(int i=0;i<array.size();i++){
                        String proxy=array.getString(i);
                        proxyArray[i]=new Proxy(proxy.split(":")[0],Integer.parseInt(proxy.split(":")[1]));
                    }
                    break;
                } catch (IOException e) {
                    try {
                        Thread.sleep(1000*2);
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }
                    e.printStackTrace();
                    continue;
                }
            }
        }
        return  proxyArray;
    }
}
